
using Abp.Domain.Repositories;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;


namespace SOEI.Solution.DomainEntities.RoomSourceVerifiy.DomainService
{
    /// <summary>
    /// 领域服务层一个模块的核心业务逻辑
    ///</summary>
    public class RoomVerifyApplyManager : DomainServiceBase, IRoomVerifyApplyManager
    {
        private readonly IRepository<RoomVerifyApply, long> _roomverifyapplyRepository;

        private readonly IRepository<RoomVerify, long> _roomVerifyRepository;

        private readonly IRoomVerifyManager _roomVerifyManager;
        /// <summary>
        /// 【RoomVerifyApply】的构造方法
        /// 通过构造函数注册服务到依赖注入容器中
        ///</summary>
        public RoomVerifyApplyManager(
            IRepository<RoomVerifyApply, long> roomverifyapplyRepository,
            IRepository<RoomVerify, long> roomVerifyRepository,
            IRoomVerifyManager roomVerifyManager
        )
        {
            _roomverifyapplyRepository = roomverifyapplyRepository;
            _roomVerifyRepository = roomVerifyRepository;
            _roomVerifyManager = roomVerifyManager;
        }

        #region -------------------------------------------------辅助工具生成---------------------------------------------- 

        /// <summary>
        /// 返回列表查询用
        /// </summary>
        /// <returns></returns>
        public IQueryable<RoomVerifyApply> QueryEntityListAsNoTracking()
        {

            var query = _roomverifyapplyRepository.GetAll().AsNoTracking()
                        .Select(x => new RoomVerifyApply
                        {
                            Id = x.Id,
                            VerifyUserID = x.VerifyUserID,
                            OrganizeID = x.OrganizeID,
                            RoomVerifyID = x.RoomVerifyID,
                            RoomVerifyCode = x.RoomVerifyCode,
                            VerifyTime = x.VerifyTime,
                            StateCode = x.StateCode,
                            Remark = x.Remark,
                            CreationTime = x.CreationTime,
                            CreatorUserId = x.CreatorUserId,
                            LastModificationTime = x.LastModificationTime,
                            LastModifierUserId = x.LastModifierUserId
                        });



            return query;
        }

        /// <summary>
        /// 【RoomVerifyApply】返回表达式数的实体信息即IQueryable类型
        /// </summary>
        /// <returns>IQueryable</returns>
        public IQueryable<RoomVerifyApply> Query()
        {
            return _roomverifyapplyRepository.GetAll();
        }
        /// <summary>
        /// 【RoomVerifyApply】返回即IQueryable类型的实体，不包含EF Core跟踪标记
        /// </summary>
        /// <returns>IQueryable</returns>
        public IQueryable<RoomVerifyApply> QueryAsNoTracking()
        {
            return _roomverifyapplyRepository.GetAll().AsNoTracking();
        }
        /// <summary>
        /// 【RoomVerifyApply】根据Id查询实体信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns>实体</returns>
        public async Task<RoomVerifyApply> FindByIdAsync(long id)
        {
            var entity = await _roomverifyapplyRepository.GetAsync(id);
            return entity;
        }
        /// <summary>
        /// 【RoomVerifyApply】检查实体是否存在
        /// </summary>
        /// <param name="id"></param>
        /// <returns>bool</returns>
        public async Task<bool> IsExistAsync(long id)
        {
            var result = await _roomverifyapplyRepository.GetAll().AnyAsync(a => a.Id == id);
            return result;
        }
        /// <summary>
        /// 【RoomVerifyApply】创建实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<RoomVerifyApply> CreateAsync(RoomVerifyApply entity)
        {
            entity.Id = await _roomverifyapplyRepository.InsertAndGetIdAsync(entity);
            return entity;
        }
        /// <summary>
        /// 【RoomVerifyApply】更新实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task UpdateAsync(RoomVerifyApply entity)
        {
            await _roomverifyapplyRepository.UpdateAsync(entity);
        }
        /// <summary>
        /// 【RoomVerifyApply】删除实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DeleteAsync(long id)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _roomverifyapplyRepository.DeleteAsync(id);
        }
        /// <summary>
        /// 【RoomVerifyApply】批量删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task BatchDelete(List<long> input)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _roomverifyapplyRepository.DeleteAsync(a => input.Contains(a.Id));
        }
        #endregion


        #region -------------------------------------------------用户自定义------------------------------------------------
        /*请在此扩展领域服务接口*/

        /// <summary>
        /// 通过关联查询获取列表查询结果
        /// </summary>
        /// <returns></returns>
        public IQueryable<RoomVerifyApply> QueryUnionEntityListAsNoTracking()
        {
            //var queryRoomVerify = this._roomVerifyManager.QueryEntityListAsNoTracking();
            //var queryRoomVerifyApply = this.QueryEntityListAsNoTracking();
            // linqforSql
            var queryRoomVerify = this._roomVerifyManager.Query();
            var queryRoomVerifyApply = this.Query();
            // 联合查询
            var result = from x in queryRoomVerifyApply
                         join b in queryRoomVerify on x.RoomVerifyID equals b.Id into dc
                         from dci in dc.DefaultIfEmpty()

                         select new RoomVerifyApply
                         {
                             Id = x.Id,
                             OrganizeID = x.OrganizeID,
                             RoomVerifyID = x.RoomVerifyID,
                             RoomVerifyCode = x.RoomVerifyCode,
                             VerifyTime = x.VerifyTime,
                             StateCode = x.StateCode,
                             Remark = x.Remark,
                             CreationTime = x.CreationTime,
                             CreatorUserId = x.CreatorUserId,
                             LastModificationTime = x.LastModificationTime,
                             LastModifierUserId = x.LastModifierUserId,
                             VerifyUserID = x.VerifyUserID,
                             RoomVerify = dci
                         };
            return result;
        }

        /// <summary>
        /// 是否存在指定条件的实体
        /// </summary>
        /// <param name="RoomVerifyID">房屋核验ID</param>
        /// <param name="OrganizeID">机构ID</param>
        /// <returns></returns>
        public async Task<bool> IsExistAsync(long RoomVerifyID, long OrganizeID)
        {
            var result = await _roomverifyapplyRepository.GetAll().AnyAsync(a => a.RoomVerifyID == RoomVerifyID && a.OrganizeID == OrganizeID);
            return result;
        }


        /// <summary>
        /// 【RoomVerifyApply】根据Id查询实体信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns>实体</returns>
        public async Task<RoomVerifyApply> FindByRoomVerifyIDAsync(long RoomVerifyID, long OrganizeID)
        {
            var entity = await this.Query().Where(a => a.OrganizeID == OrganizeID && a.RoomVerifyID == RoomVerifyID).FirstOrDefaultAsync();
            return entity;
        }
        #endregion
    }
}
